<?php
require_once (dirname(__FILE__).'/include/common.inc.php');

require_once (dirname(__FILE__).'/include/Tools/PHPMailer_v5.1/class.phpmailer.php');

class myPage extends page{
	private $code;
	private $arrPost;
	
	function __construct(){
		parent::__construct();
		
		if(isset($_POST['execute_code'])){
			$this->code = $_POST['execute_code'];
		}
		$this->arrPost = $_POST;
		if($this->code == config::exe_code_order){
			//echo 'Order';
			$row = $this->getOrder();
			
			$path = dirname(__FILE__).'/__doc/order_mail_2011_07_15.db';
			$reader = new filereader($path);
			$reader->replace('txtFirstName',$this->arrPost['txtFirstName']);
			$reader->replace('txtLastName',$this->arrPost['txtLastName']);
			$reader->replace('order_sprice',$this->arrPost['order_sprice']);
			$reader->replace('order_num',$this->arrPost['order_num']);
			$reader->replace('txtZipCode1',$this->arrPost['txtZipCode1']);
			$reader->replace('txtZipCode2',$this->arrPost['txtZipCode2']);
			$reader->replace('cmbPrefecture',$this->arrPost['cmbPrefecture']);
			
			$reader->replace('txtAddress',$this->arrPost['txtAddress']);
			$reader->replace('txtBuilding',$this->arrPost['txtBuilding']);
			
			$reader->replace('txtTel1',$this->arrPost['txtTel1']);
			$reader->replace('txtTel2',$this->arrPost['txtTel2']);
			$reader->replace('txtTel3',$this->arrPost['txtTel3']);
			$reader->replace('row_name',$row['name']);
			$reader->replace('row_model',$row['model']);
			$reader->replace('row_title',$row['title']);
			$reader->replace('txtContent',$this->arrPost['txtContent']);
			$reader->replace('order_total',$this->arrPost['order_sprice']*$this->arrPost['order_num']);
			$reader->replace('order_id',date('YmdHis'));
			$mailMessage = $reader->output();

			$this->sendMailer_order(
				$this->arrPost['txtEmail1']."@".$this->arrPost['txtEmail2'],
				$this->arrPost['txtFirstName'].' '.$this->arrPost['txtLastName'].' 様',
				$mailMessage);
				
			
		}else if($this->code == config::exe_code_contact){
			$path = dirname(__FILE__).'/__doc/contact_mail_2011_07_18.db';
			$reader = new filereader($path);
			$reader->replace('txtFirstName',$this->arrPost['txtFirstName']);
			$reader->replace('txtLastName',$this->arrPost['txtLastName']);
			$reader->replace('txtZipCode1',$this->arrPost['txtZipCode1']);
			$reader->replace('txtZipCode2',$this->arrPost['txtZipCode2']);
			$reader->replace('cmbPrefecture',$this->arrPost['cmbPrefecture']);
			$reader->replace('txtAddress',$this->arrPost['txtAddress']);
			$reader->replace('txtBuilding',$this->arrPost['txtBuilding']);
			$reader->replace('txtTel1',$this->arrPost['txtTel1']);
			$reader->replace('txtTel2',$this->arrPost['txtTel2']);
			$reader->replace('txtTel3',$this->arrPost['txtTel3']);
			$reader->replace('txtContent',$this->arrPost['txtContent']);
			$reader->replace('mail',$this->arrPost['txtEmail1']."@".$this->arrPost['txtEmail2']);
			
			$mailMessage = $reader->output();
			
			$this->sendMailer_contact($mailMessage);
		}
	}
	
	function sendMailer_order($mailto,$username,$body){
		$mail = new PHPMailer(); // defaults to using php "mail()"
			$mail->ContentType = 'text/html';  
			$mail->IsSMTP(); 
			$mail->CharSet ="utf8";
			$mail->SMTPAuth   = true;                  // enable SMTP authentication  
	        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier  
	        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server  
	        $mail->Port       = 465;                   // set the SMTP port  
		    $mail->Username   = "cowa.japan@gmail.com";     // 远程SMTP 服务器上的用户名
		    $mail->Password   = "f0099873";            // 你的远程SMTP 服务器上用户对应的密码
			//$mail->IsSendmail(); // telling the class to use SendMail transport

			$body = eregi_replace("[\]",'',$body);

			$mail->From = "cowa.japan@gmail.com";  
        	$mail->FromName = "株式会社　興和"; 

			$mail->AddAddress($mailto, $username);

			$mail->Subject = "【株）興和】ご注文メール【ご注文を受付ました(自動配信)】";

			$mail->AltBody = $body;
			
			$mail->MsgHTML($body);

			if(!$mail->Send()) {
				header("Location: http://www.cowa-japan.co.jp/send.php?type=order&error=-1");
				exit;
			} else {
				$address = 's.o.s.xmen@gmail.com';
				$mail->ClearAddresses();
				$mail->AddAddress($address, '株式会社 興和');
				if($mail->Send()){
					//重定向浏览器 
					header("Location: http://www.cowa-japan.co.jp/send.php?type=order"); //确保重定向后，后续代码不会被执行 
					exit;
				}
			}
	}
	
	function sendMailer_contact($body){
		$mail = new PHPMailer(); // defaults to using php "mail()"
			$mail->ContentType = 'text/html';  
			$mail->IsSMTP(); 
			$mail->CharSet ="utf8";
			$mail->SMTPAuth   = true;                  // enable SMTP authentication  
	        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier  
	        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server  
	        $mail->Port       = 465;                   // set the SMTP port  
		    $mail->Username   = "cowa.japan@gmail.com";     // 远程SMTP 服务器上的用户名
		    $mail->Password   = "f0099873";            // 你的远程SMTP 服务器上用户对应的密码

			$body = eregi_replace("[\]",'',$body);

			$mail->From = "cowa.japan@gmail.com";  
        	$mail->FromName = "株式会社　興和"; 

			$address = 'd.hoo.jp@gmail.com';
			$mail->AddAddress($address, '確認者');
			$address = 's.o.s.xmen@gmail.com';
			$mail->AddAddress($address, '担当者');

			$mail->Subject = "【株）興和】お客様からのお問い合わせです。";

			$mail->AltBody = $body;
			
			$mail->MsgHTML($body);

			if(!$mail->Send()) {
				header("Location: http://www.cowa-japan.co.jp/send.php?type=order&error=-1");
				exit;
			} else {
				//重定向浏览器 
				header("Location: http://www.cowa-japan.co.jp/send.php?type=contact"); //确保重定向后，后续代码不会被执行 
				exit;
			}
	}
	
	function getOrder(){
		if($this->arrPost['order_id'] == 0){
			return null;
		}
		$row; 
		try{
			$sql = "SELECT cs2_hsell.hsellId,cs2_hsell.prodId, cs2_prod.name, cs2_prod.model, cs2_hsell.title,
					cs2_prod.info,cs2_prod.barcode,cs2_prod.size_l,cs2_prod.size_w,
					cs2_prod.size_h,weight,cs2_prod.cateId,icon,iconinfo,num,contents,price,sprice,
				IFNULL((SELECT cs2_image.fileId FROM cs2_image 
					WHERE cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image') AS fileId 
				FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId)
				 WHERE (hsellId=".$this->arrPost['order_id'].")";
			
			$row = $this->database->get_one($sql);
		}catch (Exception $e) {
			$row = null;
		}
		return $row;
	}
}

$pg = new myPage();

/*
Array ( 
	[order_sprice] => 9800 
	[order_num] => 1 
	[order_id] => 3 
	[execute_code] => 4B785921-D096-4AE6-BCB1-8B349AE092D6 
	[txtFirstName] => 1 
	[txtLastName] => 2 
	[txtZipCode1] => 3 
	[txtZipCode2] => 4 
	[cmbPrefecture] => 神奈川県 
	[txtAddress] => 5 
	[txtBuilding] => 6 
	[txtTel1] => 7 
	[txtTel2] => 8 
	[txtTel3] => 9 
	[txtEmail1] => a 
	[txtEmail2] => b 
	[txtEmail_kakunin1] => c 
	[txtEmail_kakunin2] => d 
	[txtContent] => e )
*/
?>
